package com.atguigu.spzx.common.exception;

import com.atguigu.spzx.common.result.Result;
import com.atguigu.spzx.common.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;

/**
 * <p>
 * GlobalExceptionHandler
 * </p>
 *
 * @author flyduck
 * @since 2023/10/21
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    //全局异常处理
    @ExceptionHandler(Exception.class)
    public Result error(Exception e){
        log.error(e.getMessage() , e);
        return Result.fail(ResultCodeEnum.SERVICE_ERROR);
    }

    //自定义异常处理
    @ExceptionHandler(GuiguException.class)
    public Result error(GuiguException e){
        log.error(e.getMessage() , e);
        return Result.fail(e.getResultCodeEnum());
    }

    /**
     * spring security异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    public Result error(AccessDeniedException e) {
        log.error(e.getMessage() , e);
        return Result.fail(ResultCodeEnum.PERMISSION);
    }




}
